package scales.xml.equals
import scales.xml.{PullType, QName, Elem, Attribs, Attributes, Attribute, XmlItem, XmlPath, EndElem, XCC, Misc, Miscs}
import scales.xml.serializers.NamespaceContext
import scala.collection.immutable.{ Stack, Map }
import scalaz._
import Scalaz._
object BasicPaths {
type BasicPathA = (QName, Map[String, Int])
type BasicPath = List[BasicPathA]
}
import BasicPaths._
trait XmlEquals {
def qnamesEqual(context : ComparisonContext, str : String, str2 : String) = {
val sp1 = str.split(":")
val sp2 = str2.split(":")
if (sp1.size == 2 && sp2.size == 2) {
sp1(1) == sp2(1) && {
(for{ lnc <- context.leftNamespaceContext
rnc <- context.rightNamespaceContext
lns <- lnc.mappings.get(sp1(0))
rns <- rnc.mappings.get(sp2(0))
} yield lns == rns
).
getOrElse(false)
}
} else
str == str2
}
def compareTokens( context : ComparisonContext, qnameTokenComparison : Option[(ComparisonContext, String, String) => Boolean], str : String, str2 : String) =
qnameTokenComparison.
map( f =>
f(context, str, str2)
).
getOrElse(str == str2)
def compare[T : XmlComparison]( left : T, right : T) : Option[(XmlDifference[_], ComparisonContext)] =
implicitly[XmlComparison[T]].compare(true, ComparisonContext(), left, right)
def compare[T : XmlComparison]( context : ComparisonContext, left : T, right : T) : Option[(XmlDifference[_], ComparisonContext)] =
implicitly[XmlComparison[T]].compare(true, context, left, right)
}
object XmlEquals extends XmlEquals {
}
import XmlEquals._
trait ExactXmlEquals
extends DefaultItemEquals
with DefaultAttributeEquals
with DefaultAttributesEquals
with DefaultElemEquals
with ExactStreamEquals
with QNameEquals
with DefaultQNameToken {
}
object ExactXmlEquals extends ExactXmlEquals {}
trait DefaultXmlEquals
extends DefaultItemEquals
with DefaultAttributeEquals
with DefaultAttributesEquals
with DefaultElemEquals
with DefaultStreamEquals
with QNameEquals
with DefaultQNameToken
with DefaultDocLikeEquals {
}
object DefaultXmlEquals extends DefaultXmlEquals {}
import scales.utils.collection.{path => upath}
class PathAsPullTypeIterable( originalPath : XmlPath ) extends upath.AbstractPathIterator[XmlItem, Elem, XCC, PullType] {
def initialPath : XmlPath = originalPath.copy( top = upath.Top() )
def event : PullType = path.node.focus.fold(x=>x,y=>y.section)
def end = {
val el = path.tree.section
EndElem(el.name, el.namespaces) : PullType
}
}
trait TheyReallyAreIterators {
import scales.xml.{CloseablePull, XmlPull}
implicit val closeablePullIsAn = (x : CloseablePull) => x : Iterator[PullType]
implicit val xmlPullIsAn = (x : XmlPull) => x : Iterator[PullType]
}
trait StreamComparableImplicits extends TheyReallyAreIterators {
import scales.xml.{CloseablePull, XmlPull, DocLike, Doc, XmlTree}
implicit val itrPlusDocAsAnIterator = (x : (Iterator[PullType], DocLike)) => x._1 : Iterator[PullType]
implicit val xmlPathToComparable : XmlPath => StreamComparable[XmlPath] = ( x : XmlPath ) => new StreamComparable[XmlPath](x)(t => new PathAsPullTypeIterable(t))
implicit def fromStreamToStreamComparable[T <% Iterator[PullType]](t : T) : StreamComparable[T] =
new StreamComparable(t)
implicit def itrDocLikeToStreamComparable[T <% Iterator[PullType]](t : (T, DocLike)) : StreamComparable[T] = new StreamComparable( t._1 )
implicit def docWrapper(implicit bodyComp : XmlComparison[XmlTree]) : DocLikeWrapper[Doc] = new DocLikeWrapperBase[Doc, XmlTree]( identity, _.rootElem, bodyComp )
implicit def xmlPullWrapper(implicit bodyComp : XmlComparison[Iterator[PullType]]) : DocLikeWrapper[XmlPull] = new DocLikeWrapperBase[XmlPull, Iterator[PullType]]( identity, identity, bodyComp )
implicit def closeablePullWrapper(implicit bodyComp : XmlComparison[Iterator[PullType]]) : DocLikeWrapper[CloseablePull] = new DocLikeWrapperBase[CloseablePull, Iterator[PullType]]( identity, identity, bodyComp )
implicit def itrDocLikeWrapper(implicit bodyComp : XmlComparison[Iterator[PullType]]) : DocLikeWrapper[(Iterator[PullType], DocLike)] = new DocLikeWrapperBase[(Iterator[PullType], DocLike), Iterator[PullType]]( _._2, _._1, bodyComp )
}